package io.xxx.omni.osc.sync

import io.xxx.omni.osc.domain.Document
import io.xxx.omni.osc.domain.RetryDocument
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.scheduling.annotation.Async
import org.springframework.stereotype.Component

abstract class Notifier {

    abstract val name: String

    protected val log: Logger = LoggerFactory.getLogger(javaClass)

    /**
     * 通知错误给负责人
     */
    fun notifyError(context: JobContext, document: Document, retryDocument: RetryDocument, throwable: Throwable) {
        handle(context, document, retryDocument, throwable)

        val (store) = context
        log.warn("店铺${store.name}($)报文${document.sn}错误次数已经达到${retryDocument.count}", throwable)
    }

    @Async
    abstract fun handle(context: JobContext, document: Document, retryDocument: RetryDocument, throwable: Throwable)
}

@Component
class DingTalkNotifier : Notifier() {

    override val name = "钉钉"

    override fun handle(context: JobContext, document: Document, retryDocument: RetryDocument, throwable: Throwable) {
        log.info("钉钉已通知：${document.sn}")
    }

}

@Component
class WorkWechatNotifier : Notifier() {

    override val name = "企业微信"

    override fun handle(context: JobContext, document: Document, retryDocument: RetryDocument, throwable: Throwable) {
        TODO("Not yet implemented")
    }

}

@Component
class EmailNotifier : Notifier() {

    override val name = "邮件"

    override fun handle(context: JobContext, document: Document, retryDocument: RetryDocument, throwable: Throwable) {
        TODO("Not yet implemented")
    }

}